package com.yuneec.fly.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.yuneec.fly.GlobalParams;
import com.yuneec.fly.R;
import com.yuneec.fly.events.EventBusFactory;
import com.yuneec.fly.events.uiEvent.SelectedMapChangedEvent;
import com.yuneec.fly.fragments.BaseFragment;
import com.yuneec.fly.fragments.BindDroneFragment;
import com.yuneec.fly.fragments.DownloadOfflineMapFragment;
import com.yuneec.fly.fragments.HardwareMonitorFragment;
import com.yuneec.fly.fragments.ResetFragment;
import com.yuneec.fly.fragments.WifiFragment;
import com.yuneec.fly.utils.SharePrefUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

import static com.yuneec.fly.fragments.MapFragment.SELECT_MAP_TYPE_AMAP;
import static com.yuneec.fly.fragments.MapFragment.SELECT_MAP_TYPE_AUTO;
import static com.yuneec.fly.fragments.MapFragment.SELECT_MAP_TYPE_GMAP;

/**
 * Created by jw.gu on 2016/7/13.
 */
public class SystemSettingActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {
    private HashMap<String, BaseFragment> fragments;
    private RadioButton downloadMapRB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().setGravity(Gravity.RIGHT);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.system_setting);
        fragments = new HashMap<>();
        RadioButton bindRB = (RadioButton) findViewById(R.id.bindRB);
        bindRB.setOnCheckedChangeListener(this);
        downloadMapRB = (RadioButton) findViewById(R.id.downloadMapRB);
        ((RadioButton) findViewById(R.id.wifiRB)).setOnCheckedChangeListener(this);
        ((RadioButton) findViewById(R.id.downloadMapRB)).setOnCheckedChangeListener(this);
        ((RadioButton) findViewById(R.id.resetRB)).setOnCheckedChangeListener(this);
        ((RadioButton) findViewById(R.id.hardwareRB)).setOnCheckedChangeListener(this);
//        bindRB.setVisibility(View.GONE);
        findViewById(R.id.hardwareRB).setVisibility(View.GONE);
        findViewById(R.id.wifiRB).setVisibility(View.GONE);
        setDownloadMapRBVisable();
        findViewById(R.id.resetRB).performClick();
    }

    private void switchFragment(String name) {
//        if(fragments.containsKey(name)){
//            getFragmentManager().beginTransaction().replace(R.id.content,fragments.get(name)).commit();
//        }else {
        BaseFragment fragment = null;
        switch (name) {
            case "DownloadOfflineMapFragment":
                fragment = new DownloadOfflineMapFragment();
                break;
            case "BindDroneFragment":
                fragment = new BindDroneFragment();
                break;
            case "WifiFragment":
                fragment = new WifiFragment();
                break;
            case "ResetFragment":
                fragment = new ResetFragment();
                break;
            case "HardwareMonitorFragment":
                fragment = new HardwareMonitorFragment();
                break;
        }
        getFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
        fragments.put(name, fragment);
//        }
    }

    @Override
    public void finish() {
        Intent intent = new Intent(this, MainActivity.class);
        setResult(RESULT_OK, intent);
        super.finish();
        overridePendingTransition(R.anim.setting_slide_in, R.anim.setting_slide_out);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        fragments.clear();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            switch (buttonView.getId()) {
                case R.id.bindRB:
                    switchFragment("BindDroneFragment");
                    break;
                case R.id.downloadMapRB:
                    switchFragment("DownloadOfflineMapFragment");
                    break;
                case R.id.wifiRB:
                    switchFragment("WifiFragment");
                    break;
                case R.id.resetRB:
                    switchFragment("ResetFragment");
                    break;
                case R.id.hardwareRB:
                    switchFragment("HardwareMonitorFragment");
                    break;
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUpdateRecordTimeView(SelectedMapChangedEvent event) {
        setDownloadMapRBVisable();
    }

    private void setDownloadMapRBVisable() {
        int selectMapType = SharePrefUtil.getInt(this, SharePrefUtil.KEY.KEY_SELECTED_MAP_TYPE, SELECT_MAP_TYPE_AUTO);
        if (selectMapType == SELECT_MAP_TYPE_AUTO) {
            if ("CN".equals(GlobalParams.language)) {
                downloadMapRB.setVisibility(View.VISIBLE);
            } else {
                downloadMapRB.setVisibility(View.GONE);
            }
        } else if (selectMapType == SELECT_MAP_TYPE_GMAP) {
            downloadMapRB.setVisibility(View.GONE);
        } else if (selectMapType == SELECT_MAP_TYPE_AMAP) {
            downloadMapRB.setVisibility(View.VISIBLE);
        }
    }
}